package com.jdd.modules.parking.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.common.constant.JddConstant;
import com.jdd.modules.parking.entity.InParkinglotCar;
import com.jdd.modules.parking.entity.ParkArmInfo;
import com.jdd.modules.parking.entity.ParkCarEnterLog;
import com.jdd.modules.parking.entity.ParkCarOutLog;
import com.jdd.modules.parking.mapper.InParkinglotCarMapper;
import com.jdd.modules.parking.mapper.ParkCarEnterLogMapper;
import com.jdd.modules.parking.mapper.ParkCarOutLogMapper;
import com.jdd.modules.parking.service.IInParkinglotCarService;
import com.jdd.modules.parking.service.IParkArmInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: 场内车辆管理服务
 * @author:
 * @date: 2022/3/2 9:56
 */
@Slf4j
@Service
public class InParkinglotCarImpl extends ServiceImpl<InParkinglotCarMapper, InParkinglotCar> implements IInParkinglotCarService {

    @Resource
    private ParkCarOutLogMapper parkCarOutLogMapper;
    @Resource
    private ParkCarEnterLogMapper parkCarEnterLogMapper;
    @Resource
    private IParkArmInfoService parkArmInfoService;

    /**
     * 场内车辆信息-批量出场
     *
     * @param idList 车辆入场id列表
     * @param armId  车道id
     * @throws Exception 异常信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void inparkingToOutBatch(List<String> idList, String armId) throws Exception {
        for (String id : idList) {
            ParkCarEnterLog carEnterLog = parkCarEnterLogMapper.selectById(id);
            if (ObjectUtil.isEmpty(carEnterLog)) {
                throw new Exception("未查询到记录");
            }
            //滞留车转出场
            parkCarEnterLogMapper.updateIsOut(carEnterLog.getPlateNo());
            log.info("---------------场内车辆入场纪录出场成功--------------");
            //生成个出场纪录
            ParkCarOutLog parkCarOutLog = new ParkCarOutLog();
            BeanUtils.copyProperties(carEnterLog, parkCarOutLog);
            //选择出口的车道
            if (ObjectUtil.isEmpty(armId)) {
                //场内车转出场没有选择车道
                parkCarOutLog.setArmName(null);
                parkCarOutLog.setSerialNo(null);
                parkCarOutLog.setIpAddr(null);
            } else {
                //场内车转出场有选择车道
                ParkArmInfo parkArmInfo = parkArmInfoService.getById(armId);
                parkCarOutLog.setCarEnterId(carEnterLog.getId());
                parkCarOutLog.setArmName(parkArmInfo.getArmName());
                parkCarOutLog.setSerialNo(parkArmInfo.getSerialNo());
                parkCarOutLog.setIpAddr(parkArmInfo.getIpAddr());
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            parkCarOutLog.setCreateTime(new Date());
            parkCarOutLog.setCarOutTime(sdf.format(new Date()));
            parkCarOutLog.setOuterType(JddConstant.IntNumber.INT_TWO);
            parkCarOutLog.setRemark("场内车辆手动出场");
            //加上工作站名和岗亭人账户
            parkCarOutLog.setWorkName("管理员工作站");
            parkCarOutLog.setSysUserName("管理员");
            parkCarOutLogMapper.insert(parkCarOutLog);
            log.info("---------------场内车生成出场纪录出场成功--------------");
        }
    }

    /**
     * 根据批次号修改车牌号
     *
     * @param inParkingCar 入参
     * @return 修改结果
     */
    @Override
    public boolean updateByBatchNumber(InParkinglotCar inParkingCar) {
        return parkCarEnterLogMapper.updateByBatchNumber(inParkingCar) >= 0;
    }
}
